package lc;

import java.util.LinkedHashMap;

public class TwoSum {
    public int[] twoSum(int[] numbers, int target) {
    	LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
    	
    	for (int i=0; i<numbers.length; i++) {
    		map.put(numbers[i], i+1);
    	}

    	int half = (target % 2 != 0) ? -1 : target/2;
    	for (int i : map.keySet()) {
    		if (i == half) {
    			continue;
    		}
    		
    		int diff = target-i;
			if (map.containsKey(diff)) {
				int idx1=map.get(i);
				int idx2=map.get(diff);
    			return new int [] {Math.min(idx1, idx2), Math.max(idx1, idx2)};
    		}
    	}
    	
    	return new int [] {};
    }

    public static void main(String ...strings) {
    	TwoSum sol = new TwoSum();
    	int[] twoSum = sol.twoSum(new int [] {2, 7, 11, 15}, 9);
    	System.out.println(twoSum);
    	
    	twoSum = sol.twoSum(new int [] {0,4,3,0}, 0);
    	System.out.println(twoSum);
    }
}
